#import "HHBaseTableTextCell.h"
#import <BCComConfigKit/BCComConfig.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#import <Masonry/Masonry.h>
#pragma clang diagnostic pop

@interface HHBaseTableTextCell ()
@property (nonatomic, strong, readwrite) UIImageView *backgroundImgv;
@property (nonatomic, strong, readwrite) UIImageView *headImgv;
@property (nonatomic, strong, readwrite) UIImageView *headIconImgv;
@property (nonatomic, strong, readwrite) UILabel *textLb;
@property (nonatomic, strong, readwrite) UILabel *textDescribeLab;
@property (nonatomic, strong, readwrite) UILabel *detailLb;
@property (nonatomic, strong, readwrite) UIImageView *arrowImgv;
@property (nonatomic, strong, readwrite) UISwitch *rightSwitch;
@property (nonatomic, strong, readwrite) UIView *splitView;
@property (nonatomic, strong, readwrite) UIButton *rightBtn;
@end

@implementation HHBaseTableTextCell
//MARK: - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubView];
    }
    return self;
}
-(void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubView];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if ([self.delegate respondsToSelector:@selector(hh_tableTextCell:setSelected:)]) {
        [self.delegate hh_tableTextCell:self setSelected:selected];
    }
}

//MARK: - getter
-(UIImageView *)backgroundImgv {
    if (!_backgroundImgv) {
        _backgroundImgv = [[UIImageView alloc] init];
        [self.contentView insertSubview:_backgroundImgv atIndex:0];
        [_backgroundImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
    }
    return _backgroundImgv;
}
-(UIImageView *)headImgv {
    if (!_headImgv) {
        _headImgv = [[UIImageView alloc] init];
        [self.contentView addSubview:_headImgv];
        [_headImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.width.mas_equalTo(16);
            make.height.mas_equalTo(20);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return _headImgv;
}
-(UIImageView *)headIconImgv {
    if (!_headIconImgv) {
        _headIconImgv = [[UIImageView alloc] init];
        [self.contentView addSubview:_headIconImgv];
        [_headIconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.headImgv);
            make.bottom.mas_equalTo(self.headImgv);
            make.width.mas_equalTo(12);
            make.height.mas_equalTo(12);
        }];
    }
    return _headIconImgv;
}
-(UILabel *)textLb {
    if (!_textLb) {
        _textLb = [[UILabel alloc] init];
        _textLb.font = BCComConfig.config.bcFont(15);
        _textLb.textAlignment = NSTextAlignmentLeft;
        _textLb.textColor = BCComConfig.config.blackColor;
    }
    return _textLb;
}
- (UILabel *)textDescribeLab {
    if (!_textDescribeLab) {
        _textDescribeLab = [[UILabel alloc] init];
        _textDescribeLab.font = BCComConfig.config.bcFont(13);
        _textDescribeLab.textAlignment = NSTextAlignmentLeft;
        _textDescribeLab.textColor = BCComConfig.config.littleBlackColor;
        [self addSubview:_textDescribeLab];
        [_textDescribeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.textLb.mas_bottom).offset(1);
            make.left.mas_equalTo(self.textLb);
        }];
    }
    return _textDescribeLab;
}
-(UILabel *)detailLb {
    if (!_detailLb) {
        _detailLb = [[UILabel alloc] init];
        _detailLb.font = BCComConfig.config.bcFont(15);
        _detailLb.textAlignment = NSTextAlignmentRight;
        _detailLb.textColor = BCComConfig.config.littleBlackColor;
        //这里可能会导致 UITableView.automaticDimension 失效，外面需要重新调用
        //cell.detailLb.removeFromSuperview()
        //cell.contentView.addSubview(cell.detailLb)
        [self addSubview:_detailLb];
        [_detailLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-35);
            make.centerY.mas_equalTo(_detailLb.superview);
        }];
    }
    return _detailLb;
}
-(UIImageView *)arrowImgv {
    if (!_arrowImgv) {
        _arrowImgv = [[UIImageView alloc] init];
        _arrowImgv.image = [UIImage imageNamed:@"bc_arrow_right"];
        if (!_arrowImgv.image) {
            _arrowImgv.image = [UIImage imageNamed:@"arrow_right"];;
        }
        [self.contentView addSubview:_arrowImgv];
        [_arrowImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return _arrowImgv;
}
-(UISwitch *)rightSwitch {
    if (!_rightSwitch) {
        _rightSwitch = [[UISwitch alloc] init];
        [_rightSwitch setOnTintColor:BCComConfig.config.defaultColor];
        _rightSwitch.transform = CGAffineTransformMakeScale(0.8, 0.8);
        [_rightSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_rightSwitch];
        [_rightSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(38, 23));
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return _rightSwitch;
}
-(UIView *)splitView {
    if (!_splitView) {
        _splitView = [[UIView alloc] init];
        _splitView.backgroundColor = BCComConfig.config.splitColor;
        [self addSubview:_splitView];
        [_splitView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_greaterThanOrEqualTo(0);
            make.height.mas_equalTo(0.6f);
            make.bottom.mas_equalTo(0);
        }];
    }
    return _splitView;
}
- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] init];
        [_rightBtn addTarget:self action:@selector(onRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightBtn];
        [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(self);
            make.right.mas_equalTo(-5);
            make.width.mas_equalTo(40);
        }];
    }
    return _rightBtn;
}

//MARK: - 初始化
- (void)setupSubView {
    if (_textLb.superview) {
        return;
    }
    [self.contentView addSubview:self.textLb];
    [self.textLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.contentView);
    }];
}

//MARK: - Event
/// 右侧按钮点击事件
- (void)onRightBtnClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(hh_tableTextCell:selectedRightBtn:)]) {
        [self.delegate hh_tableTextCell:self selectedRightBtn:sender];
    }
}
/// switch 切换时间
- (void)switchValueChanged:(UISwitch *)sender {
    if ([self.delegate respondsToSelector:@selector(hh_tableTextCell:switchValueChanged:)]) {
        [self.delegate hh_tableTextCell:self switchValueChanged:sender.on];
    }
}
@end

